package org.flyframework.event
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.core.IMXMLObject;
	
	
	[Event(name="handle", type="flash.events.Event")]
	
	
	[Bindable]
	public class EventMapping implements IMXMLObject
	{
		
		public var controller:Controller;
		
		/*
		[Bindable("listenerChange")]
		public var listenTo:IEventDispatcher;
		[Bindable("listenerChange")]
		public var listenFor:String;
		*/
		
		public var handler:Function;
		
		public var command:Object;
		
		public var announce:Object;
		
		
		//--------------------------------------------------------------------------
		// Init
		//--------------------------------------------------------------------------
		
		public function EventMapping()
		{
			
		}
		
		
		//--------------------------------------------------------------------------
		// IMXMLObject
		//--------------------------------------------------------------------------
		
		public function initialized( document:Object, id:String ):void
		{
			// note, {binded} property values are not yet set during initialized
			trace( "EventMapping.initialized(): " + document + " " + this.listenTo + " " + this.listenFor + " " + this.handler );
			
//			this.document = document;
//			this.id = id;
		}
		
		
		//--------------------------------------------------------------------------
		// Properties
		//--------------------------------------------------------------------------
		
		private var _listenTo:IEventDispatcher;
		
		[Bindable("listenerChange")]
		public function set listenTo( value:IEventDispatcher ):void
		{
			trace( "EventMapping.listenTo set: " + value );
			
			updateListener( value, _listenFor );
		}
		
		public function get listenTo():IEventDispatcher
		{
			return _listenTo;
		}
		
		
		private var _listenFor:String;
		
		[Bindable("listenerChange")]
		public function set listenFor( value:String ):void
		{
			trace( "EventMapping.listenFor set: " + value );
			
			updateListener( _listenTo, value );
		}
		
		public function get listenFor():String
		{
			return _listenFor;
		}
		
		
		//--------------------------------------------------------------------------
		// 
		//--------------------------------------------------------------------------
		
		/*
		public function register():void
		{
			this.listenTo.addEventListener( this.listenFor, this.handler );
		}
		*/
		
		
		public function callListener( event:Event=null ):void
		{
			// TODO
			if( this.handler != null )
			{
				this.handler( event );
			}
		}
		
		
		protected function updateListener( listenTo:IEventDispatcher, listenFor:String ):void
		{
			trace( "EventMapping.updateListener: " + listenTo + " " + listenFor );
			
			// remove if already set
			if( _listenTo && _listenFor )
			{
				_listenTo.removeEventListener( _listenFor, this.callListener );
				
				trace( "   Listener removed..." );
			}
			
			_listenTo = listenTo;
			_listenFor = listenFor;
			
			// TODO: if listenTo is null? attach to controller or application?
			if( _listenTo == null ) listenTo = this.controller;
			
			// (re-)register listener
			if( _listenTo && _listenFor )
			{
				_listenTo.addEventListener( _listenFor, this.callListener );
				
				trace( "   Listener added..." );
			}
			
			dispatchEvent( new Event("listenerChange") );
		}
		
	}
	
}